<template>
	<view class="viewpage">
		<web-view :src="AcRegisterObject.webviewSrc" @message="AcRegisterObject.webviewOnMessage" ref="webviewRef" :webview-styles="{width:'0',height:'0',position: 'absolute',top: '-500',left: '-375'}"></web-view>
		<div class="ring">Loading
			<text></text>
		</div>
	</view>
</template>

<script>
	import commonApi from "@/services/api/common.js";
	const IOSXJPluginIOSPhotoModel = uni.requireNativePlugin("IOSXJPlugin-IOSPhotoModel")
	import AcRegisterClass from "./AcRegisterClass.js";
	//此页面用来配置加载项目前的配置
	export default {
		data() {
			return {
				AcRegisterObject: new AcRegisterClass({
					//组件参数 调试模式
					debug: false,
					//组件参数 主机地址
					host: 'api.imdemo.top',
					//组件参数 应用id (对应平台id)
					app_id: 1,//代表123456
					//组件参数  客户令牌
					c_api_token: 'c_api_token',
				}),
			}
		},
		onLoad() {
			//监听更新页面返回
			uni.$on('nextPage',this.nextPage);
			uni.getNetworkType({
				success: (e) => {
					if(e.networkType != 'none'){
						this.init()
					}else{
						uni.showLoading({ 
							title:this.$t("loadding.title.waitNetwork")
						})
						uni.onNetworkStatusChange((res)=>{
							if(res.isConnected){
								uni.hideLoading()
								this.init()
							}
						})
					}
				}
			})
		},
		onReady() {
			var currentWebview = this.$scope.$getAppWebview();
			var webviewObject = currentWebview.children()[0]
			this.AcRegisterObject.ready(webviewObject);
		},
		onUnload() {
			//关闭监听
			uni.$off('nextPage',this.nextPage);
		},
		methods: {
			//查找访问者id
			findVid(){
				return new Promise((resolve, reject) => {
				    this.AcRegisterObject.findVid(
				    	5,//查找时间(单位:秒) 超过该时间 无论是否查找到访问者id 都会回调函数(参数2)
				    	(vid) => {
				    		if (vid != 0) {
				    			this.AcRegisterObject.findV(vid, (v) => {
									console.log('调用findV回调:', v);
				    				if (v != '') {
										let searchParams = JSON.parse(v.v_searchParams)
										console.log('调用findV回调解析参数:', searchParams);
										// if(searchParams.p){
										// 	uni.setStorageSync('plat_code',searchParams.p)
										// }else{
										// 	uni.removeStorageSync('plat_code')
										// }
										if(searchParams.q){
											uni.setStorageSync('parent_code',searchParams.q)
										}else{
											uni.removeStorageSync('parent_code')
										}
										if(searchParams.u){
											uni.setStorageSync('user_code',searchParams.u)
										}else{
											uni.removeStorageSync('user_code')
										}
										uni.setStorageSync('weburl',v.v_fromurl)
										return resolve()
				    				}
									reject('找不到findV回调')
				    			})
				    		}else{
								reject('找不到vid')
							}
				    	}
				    );
				});
			},
			//初始化页面
			async init() {
				try{
					await uni.$config.getConfigRequest((tost)=>{
						console.log(tost);
					})
					console.log(uni.$config);
					if(!uni.$config.plat_status){
						uni.showModal({
							title:'提示',
							content:'平台已关闭',
							showCancel:false,
							success:(e)=>{
								plus.runtime.quit();
							}
						})
						return
					}
				}catch(e){
					uni.showToast({
						icon:'none',
						title: e.errMsg||e
					});
					return
				}
				console.log('正在检查版本更新');
				await commonApi.appUpdate(0)
				await this.nextPage()
			},
			//执行继续代码操作
			async nextPage(){
				console.log('正在获取推广信息');
				//获取推广信息
				try{
					await this.findVid()
				}catch(e){
					console.log(e);
				}
				console.log('正在获取设备信息');
				try{
					await this.getDeviceInfo()
				}catch(e){
					
				}
				
				try{ 
					console.log('正在初始化登录');
					await this.$store.dispatch('initLogin')
					this.$Router.pushTab({
						name: 'index'
					});
				}catch(e){
					if(uni.$config.is_reg){
						this.$Router.replaceAll({
							name: 'login'
						});
					}else{
						try{
							console.log('正在自动登录中');
							await this.$store.dispatch('autoLogin')
							this.$Router.pushTab({
								name: 'index'
							});
						}catch(e){
							
						}
					}
				}
			},
			async getDeviceInfo(){
				let systemInfo = uni.getSystemInfoSync()
				var timeZoneOffset = new Date().getTimezoneOffset();
				var timeZone = "GMT" + (timeZoneOffset > 0 ? "-" : "+") + 
				              Math.floor(Math.abs(timeZoneOffset) / 60) + 
				              ":" + (Math.abs(timeZoneOffset) % 60);
				//获取设备详情
				let fromdata = {
					plat_code: uni.$config.plat_code,
					parent_code: uni.getStorageSync('parent_code') || '0',
					device_type: systemInfo.deviceType,
					device_os: systemInfo.osName,
					device_model: systemInfo.deviceBrand + ' ' + systemInfo.deviceModel,
					device_token: '',
					device_id:'',
					browser_name: systemInfo.browserName,
					browser_version: systemInfo.browserVersion,
					device_language: systemInfo.osLanguage,
					device_timezone: timeZone,
					distributer_code:uni.getStorageSync('user_code') || "",
					domain:uni.getStorageSync('weburl') || "",
				}
				try {
					// 使用Promise封装异步操作，确保获取到设备信息后再提交
					fromdata.device_id = await new Promise((resolve, reject) => {
						IOSXJPluginIOSPhotoModel.getUDID((id) => {
							console.log('---------------',id);
							if (id) {
								resolve(id);
							} else {
								resolve('');
							}
						});
					});
					fromdata.device_token = fromdata.device_id; // 同步设置device_token
				} catch (e) {
					console.error('获取设备信息失败', e);
				}
				console.log(fromdata);
				this.$store.commit('setDeviceInfo',fromdata)
			}
		}
	}
</script>

<style lang="scss">
	.viewpage{
		height: 100vh;
		background: url(/static/qidongye.png);
		background-size: 100% 100%;
	}
	.ring {
		position: absolute;
		top: 60%;
		left: 50%;
		transform: translate(-50%, -50%);
		width: 100px;
		height: 100px;
		background: transparent;
		border: 3px solid #3c3c3c;
		border-radius: 50%;
		text-align: center;
		line-height: 100px;
		font-family: sans-serif;
		font-size: 15px;
		color: #fff000;
		letter-spacing: 4px;
		text-transform: uppercase;
		text-shadow: 0 0 10px #fff000;
		box-shadow: 0 0 20px rgba(0, 0, 0, .5);
		

		text {
			display: block;
			position: absolute;
			top: calc(50% - 2px);
			left: 50%;
			width: 50%;
			height: 4px;
			background: transparent;
			transform-origin: left;
			animation: animate 2s linear infinite;
		}

		text:before {
			content: '';
			position: absolute;
			width: 16px;
			height: 16px;
			border-radius: 50%;
			background: #fff000;
			top: -6px;
			right: -8px;
			box-shadow: 0 0 20px #fff000;
		}
	}

	.ring:before {
		content: '';
		position: absolute;
		top: -3px;
		left: -3px;
		width: 100%;
		height: 100%;
		border: 3px solid transparent;
		border-top: 3px solid #fff000;
		border-right: 3px solid #fff000;
		border-radius: 50%;
		animation: animateC 2s linear infinite;
	}



	@keyframes animateC {
		0% {
			transform: rotate(0deg);
		}

		100% {
			transform: rotate(360deg);
		}
	}

	@keyframes animate {
		0% {
			transform: rotate(45deg);
		}

		100% {
			transform: rotate(405deg);
		}
	}
</style>